<?php
session_start();
require_once("db_connect.php");

function calc_age($birth)
{
    $ty = date("Y");
    $tm = date("m");
    $td = date("d");
    list($by, $bm, $bd) = explode('-', $birth);
    $age = $ty - $by;
    if($tm * 100 + $td < $bm * 100 + $bd) $age--;
    return $age;
}

	$volunteer_information_id = 7;
	$user_id = 2;
	//$volunteer_information_id  = $_session['volunteer_information_id'];
	//$user_id  = $_session['user_id'];

	$Family_Name  = htmlspecialchars($_POST['Family_Name'],ENT_QUOTES,"UTF-8").",";
	$Given_Name  = htmlspecialchars($_POST['Given_Name'],ENT_QUOTES,"UTF-8").",";
	$Family_Kana  = htmlspecialchars($_POST['Family_Kana'],ENT_QUOTES,"UTF-8").",";
	$Given_Kana  = htmlspecialchars($_POST['Given_Kana'],ENT_QUOTES,"UTF-8").",";
	$Nick_Name  = htmlspecialchars($_POST['Nick_Name'],ENT_QUOTES,"UTF-8").",";
	$Phone_Number  = htmlspecialchars($_POST['Phone_Number'],ENT_QUOTES,"UTF-8").",";
	$eMail  = htmlspecialchars($_POST['eMail'],ENT_QUOTES,"UTF-8").",";
	$group_Name  = htmlspecialchars($_POST['group_Name'],ENT_QUOTES,"UTF-8").",";
	$group_Kana  = htmlspecialchars($_POST['group_Kana'],ENT_QUOTES,"UTF-8").",";

	$statistics_gender  = htmlspecialchars($_POST['Gender'],ENT_QUOTES,"UTF-8").",";
	$application_number  = htmlspecialchars($_POST['application_number'],ENT_QUOTES,"UTF-8").",";
	$statistics_pref  = htmlspecialchars($_POST['pref'],ENT_QUOTES,"UTF-8").",";
	$statistics_post  = htmlspecialchars($_POST['Position_ID'],ENT_QUOTES,"UTF-8").",";
	$year  = date("Y-m-d",mktime(0,0,0,$_POST['month'],$_POST['day'],$_POST['year'])).",";
	$statistics_age  = calc_age($year);
	$statistics_why  = htmlspecialchars($_POST['statistics_why'],ENT_QUOTES,"UTF-8").",";

	$sql = "INSERT INTO application(volunteer_information_id,user_id,application_number,statistics_gender,statistics_pref,statistics_post,statistics_age,statistics_why,
									Family_Name,Given_Name,Family_Kana,Given_Kana,Nick_Name,Phone_Number,eMail,group_Name,group_Kana)
			VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

	$stmt = $db->prepare($sql);

	$data = array($volunteer_information_id,
				  $user_id,
				  $application_number,
				  $statistics_gender,
				  $statistics_pref,
				  $statistics_post,
				  $statistics_age,
				  $statistics_why,
				  $Family_Name,
				  $Given_Name,
				  $Family_Kana,
				  $Given_Kana,
				  $Nick_Name,
				  $Phone_Number,
				  $eMail,
				  $group_Name,
				  $group_Kana
	);


	$db->execute($stmt, $data);     //UPDATE文を実行

	if (PEAR::isError($db)) {       //SQLの実行に失敗すればここでエラー文を出力
    	die($res->getMessage());
	}else{
	    echo "申請されました";
	}